﻿
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using RestSharp;
using SIS.Android.Services.Common;
using System;
using System.Net;

namespace SIS.Android.Activity
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme")]
    public class RegisterActivity : AppCompatActivity
    {
        private EditText _etName;
        private EditText _etPassword;
        private EditText _confirmPassword;
        private EditText _etPhone;
        private EditText _etEmail;
        private Button _btnregister;
        private RadioButton _rbMale;
        private RadioButton _rbFeMale;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Layout_register);
            InitViews();

            _btnregister.Click += Btnregister_Click;
            // Create your application here
        }

        private void Btnregister_Click(object sender, System.EventArgs e)
        {
            if (Check())
            {
                var restSharp = new RestSharpClient("http://114.116.64.59/sis/api/");
                var request = new RestRequest("RegisterUser", Method.POST);
                var postdata = new
                {
                    Name = _etName.Text,
                    PassWord = _etPassword.Text,
                    Phone = _etPhone.Text,
                    EmailAddress = _etEmail.Text,
                    Gender = _rbFeMale.Checked ? 1 : 2
                };
                var json = request.JsonSerializer.Serialize(postdata);
                request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);

                var respose = restSharp.Execute(request);

                if (respose.StatusCode == HttpStatusCode.OK)
                {
                    var v = Convert.ToInt32(respose.Content);
                    if (v > 0)
                    {
                        RunOnUiThread(() => Toast.MakeText(this, "注册成功,请登录", ToastLength.Short).Show());
                        Intent intent = new Intent(this, typeof(LoginActivity));
                        StartActivity(intent);
                    }
                    else
                    {
                        RunOnUiThread(() => Toast.MakeText(this, "注册失败", ToastLength.Short).Show());
                    }
                }
                else
                {
                    RunOnUiThread(() => Toast.MakeText(this, "注册失败" + respose.StatusCode.ToString(), ToastLength.Short).Show());
                }
            }
        }

        private bool Check()
        {
            if (string.IsNullOrWhiteSpace(_etName.Text))
            {
                RunOnUiThread(() => Toast.MakeText(this, "用户名不能为空!", ToastLength.Short).Show());
                return false;
            }
            if (string.IsNullOrWhiteSpace(_etPassword.Text))
            {
                RunOnUiThread(() => Toast.MakeText(this, "密码不能为空!", ToastLength.Short).Show());
                return false;
            }
            if (_etPassword.Text != _confirmPassword.Text)
            {
                RunOnUiThread(() => Toast.MakeText(this, "两次输入的密码不同!", ToastLength.Short).Show());
                return false;
            }
            return true;
        }

        private void InitViews()
        {
            _etName = (EditText)FindViewById(Resource.Id.et_account);
            _etPassword = (EditText)FindViewById(Resource.Id.et_password);
            _confirmPassword = (EditText)FindViewById(Resource.Id.confirm_password);
            _etPhone = (EditText)FindViewById(Resource.Id.et_phone);
            _etEmail = (EditText)FindViewById(Resource.Id.et_email);
            _rbMale = (RadioButton)FindViewById(Resource.Id.rb_male);
            _rbFeMale = (RadioButton)FindViewById(Resource.Id.rb_female);
            _btnregister = (Button)FindViewById(Resource.Id.btn_register);
        }
    }
}